package com.canto.access.component;

import com.canto.common.pojo.dto.CommonResult;
import com.canto.common.utils.JsonUtil;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * Session过期处理策略(用户被踢出后处理方法)
 *
 * @author jsryin
 * @version 2021-07-05
 */
public class SessionInformationExpiredStrategyImpl implements SessionInformationExpiredStrategy {

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        String msg = "已经在另一台机器登录，您被迫下线。" + event.getSessionInformation().getLastRequest();
        event.getResponse().setContentType("application/json;charset=UTF-8");
        event.getResponse().getWriter().write(JsonUtil.toObject(CommonResult.error(msg)));
    }

}
